/* Functions to implement calculation fields and handle the events
   from fields that requires other fields to be updated. */

function sum(col) {
    cArr = col.split('.');
    colName = cArr[2];
    tag = extractTag(colName);
    row = extractRow(colName);
    preStr = 'document.NalleForm.';
    postStr = '.value';
    result = 0;
    while (colName != '') {
        value = eval(preStr+colName+postStr);
        if (!isNaN(value) && value !="")
            result = result + parseFloat(value);
        row++;
        colName = findField(tag, -1, row)
    } // while
    return result;
} // sum()


function loopCalcFields() {
    elem_count = document.NalleForm.elements.length;
    for (var c = 0; c < elem_count; c++){
        fieldName = document.NalleForm.elements[c].name;
        srcTag = extractTag(fieldName);
        /* Get the array of the fields that must be updated */
        if (srcTag != null) {
            is_defined = eval('typeof(dzDep_'+srcTag+')')
            if (is_defined != "undefined") {
                handleCalculateField(fieldName, false);
            } // if
        }
    } // for
} // loopCalcFields


function setFieldValue(Field, FieldValue, Replace) {
    var srcTag = extractTag(Field);
    var numFmt = eval('dzNumFmt_' +srcTag);
    if (numFmt){
        var nfArr = numFmt.split('.');
	var dec = nfArr[0];
	var len = nfArr[1];
        var FieldValue = String(math.Round(FieldValue *Pow(10, dec)));
        var len = FieldValue.length;
        FieldValue = FieldValue.substring(0, len -dec) +'.' +FieldValue.substring(len -1, dec);
        if (FieldValue.length > len) {
            FieldValue = '*';
            for (i = 2; i < len; i++) FieldValue = FieldValue +'*';
        };
    }; 

    if (Replace)
        document.NalleForm.elements[Field].value = FieldValue;
    else if (document.NalleForm.elements[Field].value == "")
        document.NalleForm.elements[Field].value = FieldValue;
} // setFieldValue


function handleCalculateField(fieldName, ReplaceValue) {
    srcTag = extractTag(fieldName);
    srcGrp = extractGrp(fieldName);

    /* Get the array of the fields that must be updated */
    deps = eval('dzDep_'+srcTag);
    acount = deps.length;
    for (var c = 0; c < acount; c++) {
       if (deps[c].substr(0,8) == 'DZPSEUDO') replace_val = true;
       else  replace_val = ReplaceValue;

        /* Find the first occurrence of the tag */
        toName = findField(deps[c], -1, -1);
        /* Check that the field was found */
        if (toName != '') {
            if (extractType(toName) == 's') {
                // Single field so just update it
                result = dzCalculate(toName, eval('formula_'+extractTag(toName)));
                setFieldValue(toName, result, replace_val);
                continue; // Next step in the for loop
            } else {
                // Field is expandable, so several fields might have to be 
                // updated
                toGrp = extractGrp(toName);
                if (srcGrp == toGrp) {
                    // We are in the same group update only the same row
                    srcRow = extractRow(fieldName);
                    toName = findField(deps[c], -1, srcRow);
                    result = dzCalculate(toName,
                                         eval('formula_'+extractTag(toName)));
                    setFieldValue(toName, result, replace_val);
                } else {
                    // different group
                    toRow = extractRow(toName);
                    while (toName != '') {
                        result = dzCalculate(toName, eval('formula_'+extractTag(toName)));
                        setFieldValue(toName, result, replace_val);
                        toRow++;
                        toName = findField(deps[c], -1, toRow);
                    } // while
                } // if (srcGrp == toGrp)
            } // if (extractType() == 's')
        } // if (toName != "")
    } // for
} // handleCalculateField()
